﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using StopLight.ServiceImplementations;
using StopLight.Interfaces;
using Microsoft.Practices.SLUnity;
namespace StopLight.Logic {
    public enum StoplightColors {
        Green,
        Yellow,
        Red
    }

    public class LightChangedEventArgs : EventArgs {
        private StoplightColors currentColor;

        public StoplightColors CurrentColor {
            get { return currentColor; }
            private set { currentColor = value; }
        }

        public LightChangedEventArgs(StoplightColors color) {
            CurrentColor = color;
        }
    }

    public delegate void StoplightChangedHandler(object sender, LightChangedEventArgs e);

    public class Stoplight {
        public const StoplightColors Green = StoplightColors.Green;
        public const StoplightColors Yellow = StoplightColors.Yellow;
        public const StoplightColors Red = StoplightColors.Red;

        private StoplightColors currentColor = StoplightColors.Green;
        private ILogger logger = new NullLogger();
        public event StoplightChangedHandler Changed;

        public StoplightColors CurrentColor {
            get { return currentColor; }
        }

        [Dependency]
        public ILogger Logger {
            get { return logger; }
            set { logger = value; }
        }

        public void Next() {
            ++currentColor;
            if (currentColor > StoplightColors.Red) {
                currentColor = StoplightColors.Green;
            }
            RaiseChanged();
            logger.Write(string.Format("LIGHT CHANGED TO {0}", currentColor));
        }

        protected void RaiseChanged() {
            StoplightChangedHandler handlers = Changed;
            if (handlers != null) {
                LightChangedEventArgs e = new LightChangedEventArgs(currentColor);
                handlers(this, e);
            }
        }
    }
}
